package com.share.device.controller;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.share.device.domain.vo.WorkOrderVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.device.domain.WorkOrder;
import com.share.device.service.IWorkOrderService;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.utils.poi.ExcelUtil;
import com.share.common.core.web.page.TableDataInfo;

/**
 * 工单信息
 * 
 * @author         
 * @date 2025-06-29
 */
@RestController
@RequestMapping("/workorder")
@Tag(name = "工单信息", description = "APIs for managing Work Orders")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单信息列表
     */
    @RequiresPermissions("device:workorder:list")
    @GetMapping("/list")
    @Operation(summary = "工单信息列表")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        List<WorkOrderVO> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单信息列表
     */
    @RequiresPermissions("device:workorder:export")
    @Log(title = "工单信息", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出工单信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkOrder workOrder)
    {
        List<WorkOrderVO> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrderVO> util = new ExcelUtil<WorkOrderVO>(WorkOrderVO.class);
        util.exportExcel(response, list, "工单信息数据");
    }

    /**
     * 获取工单信息详细信息
     */
    @RequiresPermissions("device:workorder:query")
    @Operation(summary = "获取工单信息详细信息")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Integer orderId)
    {
        return success(workOrderService.selectWorkOrderById(orderId));
    }

    /**
     * 获取工单详细信息（包含关联信息）
     */
    @RequiresPermissions("device:workorder:query")
    @Operation(summary = "获取工单详细信息（包含关联信息）")
    @GetMapping(value = "/detail/{orderId}")
    public AjaxResult getDetail(@PathVariable("orderId") Integer orderId)
    {
        return success(workOrderService.getWorkOrderDetail(orderId));
    }

    /**
     * 新增工单信息
     */
    @RequiresPermissions("device:workorder:add")
    @Operation(summary = "新增工单信息")
    @Log(title = "工单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单信息
     */
    @RequiresPermissions("device:workorder:edit")
    @Operation(summary = "修改工单信息")
    @Log(title = "工单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单信息
     */
    @RequiresPermissions("device:workorder:remove")
    @Operation(summary = "删除工单信息")
    @Log(title = "工单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Integer[] orderIds)
    {
        return toAjax(workOrderService.deleteWorkOrderByIds(orderIds));
    }

    /**
     * 分配工单给处理人
     */
    @RequiresPermissions("device:workorder:assign")
    @Operation(summary = "分配工单给处理人")
    @Log(title = "分配工单", businessType = BusinessType.UPDATE)
    @PutMapping("/assign/{orderId}")
    public AjaxResult assignWorkOrder(@PathVariable("orderId") Integer orderId, @RequestParam("assigneeId") Integer assigneeId)
    {
        return toAjax(workOrderService.assignWorkOrder(orderId, assigneeId));
    }

    /**
     * 更新工单状态
     */
    @RequiresPermissions("device:workorder:edit")
    @Operation(summary = "更新工单状态")
    @Log(title = "更新工单状态", businessType = BusinessType.UPDATE)
    @PutMapping("/status/{orderId}")
    public AjaxResult updateStatus(@PathVariable("orderId") Integer orderId, @RequestParam("status") String status)
    {
        return toAjax(workOrderService.updateWorkOrderStatus(orderId, status));
    }

    /**
     * 完成工单
     */
    @RequiresPermissions("device:workorder:edit")
    @Operation(summary = "完成工单")
    @Log(title = "完成工单", businessType = BusinessType.UPDATE)
    @PutMapping("/complete/{orderId}")
    public AjaxResult completeWorkOrder(@PathVariable("orderId") Integer orderId)
    {
        return toAjax(workOrderService.completeWorkOrder(orderId));
    }

    /**
     * 根据状态查询工单列表
     */
    @RequiresPermissions("device:workorder:list")
    @Operation(summary = "根据状态查询工单列表")
    @GetMapping("/status/{status}")
    public AjaxResult listByStatus(@PathVariable("status") String status)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderListByStatus(status);
        return success(list);
    }

    /**
     * 根据处理人查询工单列表
     */
    @RequiresPermissions("device:workorder:list")
    @Operation(summary = "根据处理人查询工单列表")
    @GetMapping("/assignee/{assigneeId}")
    public AjaxResult listByAssignee(@PathVariable("assigneeId") Integer assigneeId)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderListByAssignee(assigneeId);
        return success(list);
    }

    /**
     * 根据设备ID查询工单列表
     */
    @RequiresPermissions("device:workorder:list")
    @Operation(summary = "根据设备ID查询工单列表")
    @GetMapping("/device/{deviceId}")
    public AjaxResult listByDevice(@PathVariable("deviceId") Integer deviceId)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderListByDevice(deviceId);
        return success(list);
    }

    /**
     * 获取工单统计信息
     */
    @RequiresPermissions("device:workorder:query")
    @Operation(summary = "获取工单统计信息")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Map<String, Object> statistics = workOrderService.getWorkOrderStatistics();
        return success(statistics);
    }
} 